<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>矩阵中战斗力最弱的 K 行</title>
</head>
<body>
  <script>
    let mat = [
      [1, 1, 0, 0, 0],
      [1, 1, 1, 1, 0],
      [1, 0, 0, 0, 0],
      [1, 1, 0, 0, 0],
      [1, 1, 1, 1, 1]
    ]
    let k = 3
    function fn(mat, k) {
      let arr = [];
      let Arrs = [];
      // 计算每行战斗力 同时记录行数 组成二维数组
      for (let i = 0; i < mat.length; i++) {
        arr.push([i, mat[i].filter((item) => item === 1).length]);
      }
      // 根据战斗力来进行排序
      arr.sort((a, b) => a[1] - b[1]);
      // 根据要求循环遍历取规定个数
      for (let i = 0; i < k; i++) {
        Arrs.push(arr[i][0]);
      }
      return Arrs;
    }
    console.log(fn(mat, k))
  </script>
</body>
</html>